package com.eastedu.superise.handler.question.callback.handler;

import com.eastedu.common.enums.MediaTypeEnum;
import com.eastedu.common.model.question.Fragment;
import com.eastedu.common.model.question.QuestionItemContent;
import com.eastedu.superise.handler.question.handler.ContentParser;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 解析选择题干的括号内容
 *
 * @author Administrator
 */
@Slf4j
public class StemContentRollbackParser implements ContentParser<ContentRollback> {
    private final Set<Long> questions = Sets.newConcurrentHashSet();

    @Override
    public ContentRollback parse(Long questionId, List<QuestionItemContent> contents) {
        boolean needCallback = false;
        // stem中，如果出现连续的Fragment(条件为MediaTypeEnum==FORMULA)， 且第一个Fragment中的content以‘<math’开头，中间的fragment非‘<math’开头且非‘</math>’结尾， 最后一个fragment以‘</math>’结尾
        // 则认为该steam符合条件，并记录List<QuestionItemContent>的索引和以及QuestionItemContent中的Fragment的索引。
        // 记录Map<Integer, List<Integer>>，key为List<QuestionItemContent>的索引，value为List<QuestionItemContent>中的Fragment的索引
        Map<Integer, Map<Integer, String>> formulaMap = new ConcurrentHashMap<>(10);
        boolean continueFlag = false;
        AtomicInteger index = new AtomicInteger(0);
        for (QuestionItemContent questionItemContent : contents) {
            List<Fragment> fragments = questionItemContent.getFragments();
            int random = index.getAndIncrement();
            for (int j = 0; j < fragments.size(); j++) {
                Fragment fragment = fragments.get(j);
                if (fragment.getMediaType() != MediaTypeEnum.FORMULA) {
                    if (continueFlag) {
                        random = index.getAndIncrement();
                        continueFlag = false;
                    }
                    continue;
                }
                if (fragment.getContent().startsWith("<math") && fragment.getContent().endsWith("</math>")) {
                    if (continueFlag) {
                        random = index.getAndIncrement();
                        continueFlag = false;
                    }
                    continue;
                }
                continueFlag = true;
                formulaMap.computeIfAbsent(random, k -> new ConcurrentHashMap<>(10)).putIfAbsent(j, fragment.getContent());
            }
        }
        // 计算formulaMap中的value的key是否为连续的数字
        for (Map.Entry<Integer, Map<Integer, String>> entry : formulaMap.entrySet()) {
            Map<Integer, String> value = entry.getValue();
            if (value.size() < 2) {
                continue;
            }
            List<Integer> keys = new ArrayList<>(value.keySet());
            keys.sort(Integer::compareTo);
            // 判断keys是否为连续的数字
            boolean continuous = true;
            for (int i = 0; i < keys.size() - 1; i++) {
                if (keys.get(i + 1) - keys.get(i) != 1) {
                    continuous = false;
                    break;
                }
            }
            if (continuous) {
                needCallback = true;
                // 添加试题
                questions.add(questionId);
                break;
            }
        }

        return new ContentRollback(formulaMap, needCallback);
    }

    @Override
    public void print() {
        log.info("括号异常的试题: {}", questions);
    }
}
